var box = document.querySelector('.list_box')

    var str = ''

    list.forEach(function (item) {
      str += `
        <li>
          <img src="${ item.img }" alt="">
         
         
          <p class="p_word"> ${ item.title }</p>
          <p class="p_price">￥: ${ item.price }</p>
          <p class="add">
        <button>加入购物车</button>
        </p>
        </li>
      `
    })

    box.innerHTML = str
   


    ////搜索引擎
    const inp =document.querySelector('input')
    const ulBox=document.querySelector('.u_search')

    inp.oninput=function(){
   const text =inp.value.trim()
    const script =document.createElement('script')
    script.src=`https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&sugsid=34443,35104,35237,35049,34584,34518,35234,34579,34606,34815,26350,35145,22159&wd=${ text }&req=2&csor=1&cb=handler&_=1637892915435`
   document.body.appendChild(script)
   script.remove()
    }
  

    function handler(res){
        if(!res.g){
            ulBox.style.display='none'
            return
        }
         let str =''
         res.g.forEach(item => {
             str+=` <li>${ item.q }</li>`
             
         })
         ulBox.innerHTML=str
  ulBox.style.display='block'
    }

//
box.addEventListener('click', e => {
  
  if (e.target.nodeName === 'LI') {
    const title = e.target.dataset.title
    window.sessionStorage.setItem('title', title)


    window.location.href = '../src/views/detail.html'
  }


  if (e.target.nodeName === 'BUTTON') {

    const title = e.target.dataset.title
    
    addCart(title)
  }
})


async function addCart(goodsId) {
  const id = window.localStorage.getItem('id')
  const token = window.localStorage.getItem('token')

  if (!id) {
    const res = window.confirm('您还没有登录, 请登录后在购买, 点击确定跳转回登录页, 点击取消留在当前页面')
    if (res) {
      window.location.href = './login.html'
      return
    }
    return
  }

  const res = await pAjax({
    url: 'http://localhost:8888/cart/add',
    method: 'POST',
    data: { id, goodsId },
    headers: { authorization: token },
    dataType: 'json'
  })

  if (res.code === 401) {
    const res = window.confirm('您的登录已经过期, 请重新登录后在购买, 点击确定跳转回登录页, 点击取消留在当前页面')
    if (res) {
      window.location.href = './login.html'
      return
    }
    return
  }

  if (res.code === 1) {
    alert('加入购物车成功')
  }
}






// 显示xxx登录 

const id = window.localStorage.getItem('id')
const token = window.localStorage.getItem('token')

const onBox = document.querySelector('.on')
const offBox = document.querySelector('.user_name')
console.log(onBox)
console.log(offBox)
if (!id) {
  onBox.classList.remove('active')
  offBox.classList.add('active')
}

const xhr = new XMLHttpRequest()

xhr.open('GET', 'http://localhost:8888/users/info?id=' + id)

xhr.onload = function () {
  const res = JSON.parse(xhr.responseText)
  if (res.code === 401) {
    onBox.classList.remove('active')
    offBox.classList.add('active')
  }

  if (res.code === 1) {
    onBox.firstElementChild.innerText = res.info.nickname
    offBox.classList.remove('active')
    onBox.classList.add('active')
  }
}

xhr.setRequestHeader('authorization', token)

xhr.send()

const logoutBtn = document.querySelector('.logout')
logoutBtn.addEventListener('click', async () => {
  const res = await pAjax({
    url: 'http://localhost:8888/users/logout',
    data: { id },
    dataType: 'json'
  })

  if (res.code === 1) {
    window.localStorage.removeItem('id')
    window.localStorage.removeItem('token')

    window.location.reload()
  }
})






